package com.jinrustar.shenjiying.util

import android.content.Context
import android.util.Log
import android.util.TypedValue
import android.widget.Toast
import java.text.SimpleDateFormat
import java.util.*

/**
 * Created by wei on 17-12-12.
 */
object CommonUtil {

    /**
     * dp转为px
     *
     * @param context  上下文
     * @param dipValue dp值
     * @return
     */
    fun dip2px(context: Context, dipValue: Float): Int {
        val r = context.resources
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, r.displayMetrics).toInt()
    }


    fun getStatusBarHeight(context: Context): Int {
        var statusBarHeight = -1
        //获取status_bar_height资源的ID
        val resourceId = context.resources.getIdentifier("status_bar_height", "dimen", "android")
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight = context.resources.getDimensionPixelSize(resourceId)
        }
        return statusBarHeight
    }


    /*反射获取状态栏高度*/
    fun getStatusBarHeight2(context: Context): Int {
        var statusBarHeight = -1
        try {
            val clazz = Class.forName("com.android.internal.R\$dimen")
            val `object` = clazz.newInstance()
            val height = Integer.parseInt(clazz.getField("status_bar_height")
                    .get(`object`).toString())
            statusBarHeight = context.resources.getDimensionPixelSize(height)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return statusBarHeight
    }


    fun randomStr(len: Int): String {
        val s = StringBuilder()
        for (i in 0..(len-1)) {/*包含结束值*/
            val r = (Math.random() * 100).toInt() % 36
            s.append(if (r < 10) r else (if ((Math.random() * 10).toInt() % 2 == 0) 'A' else 'a') + (r - 10))
        }

        return s.toString()
    }


    fun getFormatDateNow(pattern: String): String {
        val sdf = SimpleDateFormat(pattern)
        return sdf.format(Date())
    }
}